require 'map_editor/palet_notebook'
module MapEditor
  class PaletContainer < Gtk::VBox
    attr_reader :palet_notebook
    def initialize
      super
      self.sensitive = false
      @zoom = 2
      
      hbox = Gtk::HBox.new
      
      hbox.add(create_common_toolbar)

      @main_mode = :draw
      @draw_mode = :pen
      @palet_set_mode = :palet_base_setter
      
      @palet_tools = Gtk::Notebook.new
      @palet_tools.show_tabs = false
      #@palet_tools.append_page(self.create_pen_toolbar)
      #@palet_tools.append_page(self.create_tile_toolbar)
      
      hbox.add(@palet_tools)
      
      # Bottom part
      @palet_notebook = PaletNotebook.new

      @palet_notebook.signal_connect("switch-page") do |obj, page, page_num|
        self.refresh(page_num)
      end
      
      t = Gtk::Table.new(1, 2)  
      t.attach(hbox, 0, 1, 1, 2, Gtk::EXPAND | Gtk::FILL, 0)
      t.attach(@palet_notebook, 0, 1, 2, 3, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL)
      self.add(t)
    end
    
    def create_common_toolbar
      toolbar = Gtk::Toolbar.new

      b = add_tool_button(toolbar, "Zoom In", "icon/zoom_in.xpm") do
        @zoom += 1 if @zoom < 10
        self.refresh
      end

      add_tool_button(toolbar, "Zoom Out", "icon/zoom_out.xpm") do
        @zoom -= 1 if @zoom > 1
        self.refresh
      end
      
      toolbar.append_space
      tool_buttons = {}      
      b = add_tool_toggle_button(tool_buttons, toolbar, "icon/pen.xpm", "pen", :drawing) do
        @main_mode = :draw
        @draw_mode = :pen
        self.refresh
      end
      b.active = true
      @main_mode = :draw
      add_tool_toggle_button(tool_buttons, toolbar, "icon/rect.xpm", "rect", :drawing) do
        @main_mode = :draw
        @draw_mode = :rect
        self.refresh
      end
      add_tool_toggle_button(tool_buttons, toolbar, "icon/fill.xpm", "fill", :drawing) do
        @main_mode = :draw
        @draw_mode = :fill
        self.refresh
      end

      toolbar.append_space
      
      b = add_tool_toggle_button(tool_buttons, toolbar, "icon/block.xpm", "Tile Base", :drawing) do
        @main_mode = :palet_set
        @palet_set_mode =:palet_base_setter
        self.refresh
      end
      b = add_tool_toggle_button(tool_buttons, toolbar, "icon/four_blocks.xpm", "Tile Collision", :drawing) do
        @main_mode = :palet_set
        @palet_set_mode = :palet_collision_setter
        self.refresh
      end
      return toolbar
    end

    def mode=(mode)
      @main_mode = mode
      self.refresh
    end
    
    def clear
      @palet_notebook.clear
      self.sensitive = false
    end
    
    def refresh(page = @palet_notebook.page)
      case(@main_mode)
        when :draw
          @palet_tools.page = 0
          @palet_notebook.set_panel_mode(page, :palet_picker, @zoom)
        when :palet_set
          @palet_tools.page = 1
          @palet_notebook.set_panel_mode(page, @palet_set_mode, @zoom)
      end
    end
    
    def active=(value)
      self.sensitive = value
    end
    
    def on_resize(width, height)
      @palet_notebook.on_resize(width, height - 32)
    end
  end
end